package com.boyaa.shells.tool.sliderContorl 
{
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	import gs.TweenLite;
	/**
	 * ...
	 * @author Fly
	 */
	public class Slider 
	{
		
		protected var _cont:DisplayObjectContainer;
		protected var _numperpage:int;
		protected var _itemLength:int;
		protected var _isrow:Boolean = false;
		protected var _itemArr:Array = null;
		private var pt:Point;
		private var _index:int;
		private var _infoArr:Array;
		
		public function Slider() 
		{
			_infoArr = [];
			pt = new Point();
		}
		/**
		 *  初始化数据
		 * @param	numperpage
		 * @param	mc
		 * @param	isrow
		 * @param	itemLength
		 */
		public function initInfor(data:Array, numperpage:int , mc:DisplayObjectContainer, isrow:Boolean, itemLength:int) :void
		{
			_infoArr = data;
			_numperpage = numperpage;
			_cont = mc;
			_itemLength = itemLength;
			_isrow = true;
			initItem(_infoArr);
		}
		
		/**
		 *  初始化小格子
		 * @param	arr  
		 */
		private function initItem(arr:Array):void
		{
			_itemArr = [];
			for (var i:int = 0; i <= _numperpage; i++) 
			{
				var item:BasicItem = getItem();
				_itemArr.push(item);
				item.addEvent();
				item.move(i * _itemLength, 0);
				item.update(arr[i], i);
			}
		}
		
		public function toTween(index:int):void
		{
			pt.x = _index * _itemLength;
			if (index > _index)
				_add = true;
			else	
				_add = false;
			_index = index;
			TweenLite.to(pt, 1, { x:_index * _itemLength , onUpdate:updatMoveScroll } );
		}
		private var _add:Boolean = false;
		private function completeScroll():void 
		{
			var offset:int = 0;
			var pre:int = _index;
			if (_add)
			{
				offset = _itemLength;
				pre --;
			}
			move(_infoArr.slice(pre, pre + _numperpage + 1), offset, pre);
		}
		
		private function updatMoveScroll():void 
		{
			
			var index:int = pt.x / _itemLength;
			if (index == _index)
			{
				completeScroll();
				return;
			}
			var offset:int = pt.x % _itemLength;
			
			var pre:int = index;
			if (!_add)
			{
				if (pre != 0)
					pre --;
			}
			move(_infoArr.slice(pre, pre + _numperpage + 1), offset, pre);
		}
		
		/**
		 *  滚动
		 * @param	arr
		 * @param	OFFSET
		 * @param	index
		 */
		private function move(arr:Array ,OFFSET:int,index:int):void
		{
			for (var i:int = 0; i < _itemArr.length ; i++) 
			{
				var item:BasicItem = _itemArr[i];
				item.move( -OFFSET + _itemLength * i, 0);
				item.update(arr[i], index + i);
			}
		}
		
		
		protected function getItem():BasicItem 
		{
			return null;	
		}
		
	}

}